import {Injectable} from "@angular/core";

@Injectable({
  providedIn: 'root',
})
export class LocalStorageService {
  public localstorage: any;

  public set(key: string, value: string): void {
    this.localstorage[key] = value;
  }

  public get(key: string): string {
    return this.localstorage[key] || false;
  }

  public setObject(key: string, value: any): void {
    this.localstorage[key] = JSON.stringify(value);
  }

  public getObject(key: string): any {
    return JSON.parse(this.localstorage[key] || '{}');
  }

  public remove(key: string): any {
    this.localstorage.removeItem(key);
  }

  public removeAll(): void {
    this.localstorage.clear();
  }

  constructor() {
    if (!sessionStorage) {
      throw new Error('Current browser does not support Local Storage');
    }
    this.localstorage = sessionStorage;
  }
}
